/**
 实现柱形图
 */
package com.demo.kafka.flink.shop;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import javax.swing.*;
import java.awt.*;

public class BarChart {
    //1-写一个ChartPanel变量
    ChartPanel jframe;

    //2-BarChart的无参数的构造方法
    public BarChart(String time,int a,int b,int c,int d,int e) {
        DefaultCategoryDataset data=(DefaultCategoryDataset) getDataSet(a,b,c,d,e);
        JFreeChart chart= ChartFactory.createBarChart3D(
                time + "手机实时销售数据统计",  //图表标题
                "手机品牌",//目录轴的显示标签
                "数量",//数值轴的显示标签
                data,
                PlotOrientation.VERTICAL,  //图表方向 水平 垂直
                true,  //是否显示图例(对于简单的图表建议显示图例)
                false,//是否生成工具
                false);  //是否生成网址链接
        //字体设置
        //获得图表区域对象
        CategoryPlot plot=chart.getCategoryPlot();
        //水平底部列表
        CategoryAxis domain =plot.getDomainAxis();
        //垂直标题字体设置
        domain.setTickLabelFont(new Font("黑体", Font.BOLD, 16));
        //水平底部标题设置
        domain.setLabelFont(new Font("黑体", Font.BOLD, 20));
        //获取柱状体
        ValueAxis rangeAxis=plot.getRangeAxis();
        rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 16));
        //设置lengend字体
        chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 16));
        chart.getTitle().setFont(new Font("黑体", Font.BOLD, 16));
        //显示数值
        BarRenderer3D renderer = new BarRenderer3D();//3D属性修改
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelsVisible(true);
        renderer.setBaseItemLabelPaint(Color.BLUE);//设置数值颜色，默认黑色
        plot.setRenderer(renderer);
        //初始化Jframe
        jframe=new ChartPanel(chart);


    }

    //3-图表数据设置
    public static CategoryDataset getDataSet(int a,int b,int c,int d,int e) {
        DefaultCategoryDataset data=new DefaultCategoryDataset();
        //设置数据
        data.setValue(a, "国产", "华为");
        data.setValue(b, "国产", "小米");
        data.setValue(c, "国产", "OPPO");
        data.setValue(d, "国外", "Samsung");
        data.setValue(e, "国外", "iPhone");

        return data;
    }

    //4-返回一个ChartPanel
    public ChartPanel getPanel() {
        return jframe;
    }
    public static void main(String time,int a,int b,int c, int d,int e) throws InterruptedException {
        JFrame j=new JFrame();
        JDialog jd=new JDialog();
        jd.setBounds(250, 250, 600, 600);
        jd.add(new BarChart(time,a,b,c,d,e).getPanel());
        jd.setVisible(true);
        jd.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    }
}